// pages/tabs/profile/profile.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 用户信息
    userInfo: {
      name: "木鑫",
      role: "供应链管理员",
      avatar: "", // 暂时设为空，避免加载不存在的图片
    },

    // 统计数据
    stats: {
      favorites: 0,
      footprint: 0,
    },

    // 订单状态
    orderStatus: [
      {
        type: "pending",
        label: "待付款",
        icon: "time",
        color: "#ff6b6b",
        count: 0,
      },
      {
        type: "paid",
        label: "待发货",
        icon: "shop",
        color: "#4ecdc4",
        count: 0,
      },
      {
        type: "shipped",
        label: "待收货",
        icon: "cart",
        color: "#45b7d1",
        count: 0,
      },
      {
        type: "completed",
        label: "已完成",
        icon: "check-circle",
        color: "#6c5ce7",
        count: 0,
      },
      {
        type: "after-sale",
        label: "售后",
        icon: "service",
        color: "#f9ca24",
        count: 0,
      },
    ],

    // 积分优惠券
    rewards: {
      points: 0,
      coupons: 0,
      cards: 0,
    },

    // 菜单项
    menuItems: [
      {
        id: "favorites",
        label: "我的收藏",
        icon: "heart",
        color: "#ff6b6b",
        path: "/pages/profile/favorites",
      },
      {
        id: "coupons",
        label: "我的优惠券",
        icon: "discount",
        color: "#4ecdc4",
        path: "/pages/profile/coupons",
      },
      {
        id: "cards",
        label: "我的卡券",
        icon: "wallet",
        color: "#45b7d1",
        path: "/pages/profile/cards",
      },
      {
        id: "leader",
        label: "领券中心",
        icon: "gift",
        color: "#6c5ce7",
        path: "/pages/profile/coupon-center",
      },
      {
        id: "address",
        label: "收货地址",
        icon: "location",
        color: "#f9ca24",
        path: "/pages/profile/address",
      },
      {
        id: "phone",
        label: "绑定手机号",
        icon: "mobile",
        color: "#ff8a80",
        path: "/pages/profile/bind-phone",
      },
      {
        id: "distribution",
        label: "分销中心",
        icon: "share",
        color: "#26d0ce",
        path: "/pages/profile/distribution",
      },
      {
        id: "category",
        label: "分类",
        icon: "view-module",
        color: "#a29bfe",
        path: "/pages/category/index",
      },
    ],

    // 更多功能
    moreFunctions: [
      {
        id: "profile",
        title: "个人资料",
        icon: "user",
        path: "/pages/profile/personal-info",
      },
      {
        id: "cache",
        title: "清除缓存",
        icon: "delete",
        path: "",
      },
      {
        id: "service",
        title: "客服",
        icon: "service",
        path: "/pages/profile/customer-service",
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.loadUserData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 页面显示时刷新数据
    this.refreshData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.refreshData().finally(() => {
      wx.stopPullDownRefresh();
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: "斯凡供应链管理系统",
      path: "/pages/tabs/home/home",
    };
  },

  /**
   * 加载用户数据
   */
  async loadUserData() {
    try {
      // 这里将在后续任务中实现具体的数据加载逻辑
      // 目前使用模拟数据
      this.setData({
        "stats.favorites": 0,
        "stats.footprint": 0,
        "rewards.points": 0,
        "rewards.coupons": 0,
        "rewards.cards": 0,
      });
    } catch (error) {
      console.error("加载用户数据失败:", error);
    }
  },

  /**
   * 刷新页面数据
   */
  async refreshData() {
    await this.loadUserData();
    await this.loadOrderCounts();
  },

  /**
   * 加载订单数量
   */
  async loadOrderCounts() {
    try {
      // 这里将在后续任务中实现具体的订单数据加载逻辑
      const orderCounts = {
        pending: 0,
        paid: 0,
        shipped: 0,
        completed: 0,
        "after-sale": 0,
      };

      const orderStatus = this.data.orderStatus.map((item) => ({
        ...item,
        count: orderCounts[item.type] || 0,
      }));

      this.setData({ orderStatus });
    } catch (error) {
      console.error("加载订单数量失败:", error);
    }
  },

  /**
   * 统计数据点击事件
   */
  onStatTap(e) {
    const type = e.currentTarget.dataset.type;
    const paths = {
      favorites: "/pages/profile/favorites",
      footprint: "/pages/profile/footprint",
    };

    if (paths[type]) {
      wx.navigateTo({
        url: paths[type],
      });
    }
  },

  /**
   * 查看所有订单
   */
  onViewAllOrders() {
    wx.navigateTo({
      url: "/pages/order/list",
    });
  },

  /**
   * 订单状态点击事件
   */
  onOrderStatusTap(e) {
    const type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: `/pages/order/list?status=${type}`,
    });
  },

  /**
   * 积分优惠券点击事件
   */
  onRewardTap(e) {
    const type = e.currentTarget.dataset.type;
    const paths = {
      points: "/pages/profile/points",
      coupons: "/pages/profile/coupons",
      cards: "/pages/profile/cards",
    };

    if (paths[type]) {
      wx.navigateTo({
        url: paths[type],
      });
    }
  },

  /**
   * 菜单项点击事件
   */
  onMenuTap(e) {
    const path = e.currentTarget.dataset.path;
    if (path) {
      wx.navigateTo({
        url: path,
      });
    }
  },

  /**
   * 更多功能点击事件
   */
  onMoreFunctionTap(e) {
    const path = e.currentTarget.dataset.path;

    if (path === "") {
      // 清除缓存功能
      wx.showModal({
        title: "提示",
        content: "确定要清除缓存吗？",
        success: (res) => {
          if (res.confirm) {
            this.clearCache();
          }
        },
      });
      return;
    }

    if (path) {
      wx.navigateTo({
        url: path,
      });
    }
  },

  /**
   * 清除缓存
   */
  clearCache() {
    wx.showLoading({
      title: "清除中...",
    });

    setTimeout(() => {
      wx.hideLoading();
      wx.showToast({
        title: "清除成功",
        icon: "success",
      });
    }, 1500);
  },
});
